package org.jeecg.common.util;

import lombok.Builder;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Builder
public class DateUtil {

    public int compareToInt(String startMonth,String endMonth) {
        startMonth = startMonth +"-01";
        endMonth = endMonth +"-01";
        DateTimeFormatter dateFormat = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime start = dateFormat.parseDateTime(startMonth);
        DateTime end = dateFormat.parseDateTime(endMonth);
        int months = Months.monthsBetween(start, end).getMonths();
        return months;
    }
    public  String getMinMonthDate(String date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = date+"-01";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateFormat.parse(date));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dateFormat.format(calendar.getTime());
    }
    public  String getMaxMonthDate(String date) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = date+"-01";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateFormat.parse(date));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dateFormat.format(calendar.getTime());
    }

    public String addMonth(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(date));
        calendar.add(Calendar.MONTH, 1);
        return simpleDateFormat.format(calendar.getTime()) ;
    }
}
